package com.okilo.online.app.tools.manager

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import com.okilo.online.app.tools.log.OkiloLog
import com.okilo.online.app.weight.toast.OkiloToast

/**
 * 加载对话框管理器
 */
object OkiloToastManager {
    @SuppressLint("StaticFieldLeak")
    private var toast: OkiloToast? = null

    /**
     * 显示长时Toast
     */
    fun makeToast(context: Context?, content: String) {
        showLongText(context, content)
    }

    /**
     * 显示短时Toast (资源ID)
     */
    fun showShortText(context: Context?, resId: Int) {
        context?.let {
            try {
                showShortText(it, it.getString(resId))
            } catch (e: Exception) {
                OkiloLog.e(e.message)
            }
        }
    }

    /**
     * 显示短时Toast (文本)
     */
    fun showShortText(context: Context?, text: CharSequence?) {
        showToast(context, text, Toast.LENGTH_SHORT)
    }

    /**
     * 显示长时Toast (资源ID)
     */
    fun showLongText(context: Context?, resId: Int) {
        context?.let {
            try {
                showLongText(it, it.getString(resId))
            } catch (e: Exception) {
                OkiloLog.e(e.message)
            }
        }
    }

    /**
     * 显示长时Toast (文本)
     */
    fun showLongText(context: Context?, text: CharSequence?) {
        showToast(context, text, Toast.LENGTH_LONG)
    }

    /**
     * 显示自定义时长Toast (资源ID)
     */
    fun showTextDuration(context: Context?, resId: Int, time: Int) {
        context?.let {
            try {
                showTextDuration(it, it.getString(resId), time)
            } catch (e: Exception) {
                OkiloLog.e(e.message)
            }
        }
    }

    /**
     * 显示自定义时长Toast (文本)
     */
    fun showTextDuration(context: Context?, text: CharSequence?, time: Int) {
        if (context == null || text.isNullOrEmpty()) return

        cancelCurrentToast()

        toast = OkiloToast(context, text.toString(), time)

        if (context is Activity) {
            context.runOnUiThread {
                showToastWithAutoCancel(time)
            }
        } else {
            Handler(Looper.getMainLooper()).post {
                showToastWithAutoCancel(time)
            }
        }
    }

    /**
     * 取消当前显示的Toast
     */
    fun cancel() {
        toast?.cancel()
        toast = null
    }

    // 私有方法
    private fun showToast(context: Context?, text: CharSequence?, duration: Int) {
        if (context == null || text.isNullOrEmpty()) return

        cancelCurrentToast()

        toast = OkiloToast(context, text.toString(), duration)
        toast?.show()
    }

    private fun cancelCurrentToast() {
        toast?.cancel()
        toast = null
    }

    private fun showToastWithAutoCancel(time: Int) {
        toast?.show()
        Handler(Looper.getMainLooper()).postDelayed({
            cancel()
        }, time.toLong())
    }
}